package cn.yl.conSumerHystrix.controller;

import cn.yl.conSumerHystrix.service.ProviderService;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer")
//@DefaultProperties(defaultFallback = "global_FallbackMethod")
public class ConsumerController {

    private final ProviderService providerService;

    public ConsumerController(ProviderService providerService) {
        this.providerService = providerService;
    }

    @GetMapping("/hystrix/ok")
//    @HystrixCommand
    public String hystrixOk() {
        return providerService.getHystrixOK();
    }


    @GetMapping("/hystrix/self/timeout")
//    @HystrixCommand
    public String selfTimeOut(){
        return providerService.getHystrixOK();
    }


    @GetMapping("/hystrix/timeout")
//    @HystrixCommand
    public String providerFeignTimeOut() {
        System.out.println("providerFeignTimeOut--------------->");
        return providerService.providerHystrixTimeOut();
    }


    @HystrixCommand(fallbackMethod ="global_FallbackMethod",commandProperties = {
            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "10000"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "30000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50")
    })
    @GetMapping("hystrix/order/{id}")
    public String providerOrder(@PathVariable Long id){
        return providerService.providerOrder(id);
    }


    public String global_FallbackMethod(Long id){
        return "Global全局降级";
    }

}
